﻿#ifndef WRWORKTCPCOMMPULSEPACKET_H
#define WRWORKTCPCOMMPULSEPACKET_H

#include "WRWorkTcpCommConsts.h"
#include "../WRSoft-Utility/Runtime/WRDataPacket.h"

namespace WRWorkTcpComm
{
    namespace Priv
    {
        class PulsePacket
        {
        private:
            WRDataPacket* m_val;
            Q_DISABLE_COPY(PulsePacket)
        public:
            //初始化数据包。
            inline PulsePacket()
            {
                m_val = WRDataPacket::allocate<24>();
                {
                    char* p;
                    m_val->buffer(&p);

                    WRBytes::write((WR32u)WRWorkTcpComm::FrameHeader, p);//帧头
                    WRBytes::write((WR32s)(24), p + 4);//帧长度
                    WRBytes::write((WR16u)(0xFFFF), p + 8);//全局指令
                    WRBytes::write((WR16u)WRWorkTcpComm::Pulse, p + 10);//类型（心跳）
                    WRBytes::write((WR32u)WRWorkTcpComm::FrameVersion, p + 16);//版本
                    WRBytes::write((WR32u)WRWorkTcpComm::FrameTailor, p + 20);//帧尾
                }
            }
            //释放当前对象所占用的相关资源。
            inline ~PulsePacket()
            {
                m_val->release();
            }
            //获取数据包。
            inline WRDataPacket* operator()()
            {
                m_val->addRef();
                return m_val;
            }
            //获取数据包值。
            static WRDataPacket* value(void)
            {
                static Priv::PulsePacket val;
                return val();
            }
        };
    }

    //获取心跳数据包。
    inline WRDataPacket* pulsePacket(void)
    {
        return Priv::PulsePacket::value();
    }
}

#endif // WRWORKTCPCOMMPULSEPACKET_H
